﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using stats.quirli.Model;
using System.Net;

namespace stats.quirli
{
    /// <summary>
    /// This log allows to log the following actions
    /// - a media file has been loaded by the user(the media file URL is logged)
    /// - the player has been loaded with a query (the query is logged)
    /// - the user opened the share/store dialog (the resulting query is logged)
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class Log : System.Web.Services.WebService
    {
        /// <summary>
        /// Called for logging, when a media file has been loaded by the user (the media file URL is logged)
        /// </summary>
        /// <param name="mediaUrl">The media URL</param>
        [WebMethod]
        public void MediaLoadedByUser(String mediaUrl)
        {
            var entities = new quirlistatsEntities();
            var log = new stats.quirli.Model.Log();
            log.ActionCode =  (int) stats.quirli.Model.Log.LogActionCode.MediaLoadedByUser;
            PopulateClientInformation(log);
            log.Parameter = mediaUrl;
            entities.Logs.AddObject(log);
            entities.SaveChanges();
        }

        private void PopulateClientInformation(Model.Log log)
        {
            log.OriginatingIp = IPAddress.Parse(Context.Request.UserHostAddress).GetAddressBytes();
            log.UserAgent = Context.Request.UserAgent;
            log.Timestamp = DateTime.UtcNow;
        }

        /// <summary>
        /// Called for logging, when the player has been loaded with a query (the query is logged)
        /// </summary>
        /// <param name="mediaUrl">The query</param>   
        [WebMethod]
        public void PlayerLoadedWithQuery(String query)
        {
            var entities = new quirlistatsEntities();
            var log = new stats.quirli.Model.Log();
            log.ActionCode = (int)stats.quirli.Model.Log.LogActionCode.PlayerLoadedWithQuery;
            PopulateClientInformation(log);
            log.Parameter = query;
            entities.Logs.AddObject(log);
            entities.SaveChanges();
        }

        /// <summary>
        /// Called for logging, when the user opened the share/store dialog (the resulting shareable query is logged)
        /// </summary>
        /// <param name="mediaUrl">The shareable query</param>    
        [WebMethod]
        public void SharingTrack(String shareableQuery)
        {
            var entities = new quirlistatsEntities();
            var log = new stats.quirli.Model.Log();
            log.ActionCode = (int)stats.quirli.Model.Log.LogActionCode.SharingTrack;
            PopulateClientInformation(log);
            log.Parameter = shareableQuery;
            entities.Logs.AddObject(log);
            entities.SaveChanges();
        }
    }
}
